using GTA;
using GTA.Math;
using GTA.Native;

namespace CustomPeds;

public static class AnimationHelper
{
    public static void TaskPlayAnim(this Ped ped, string animDict, string animFile, int duration, bool Line = false, int flag = 0)
    {
        // 请求动画字典后，使用 SHVDN3 文档化 API播放动画（签名与 DressingScript 一致）
        Function.Call((Hash)0xD3BD40951412FEF6ul, animDict);
        while (!Function.Call<bool>((Hash)0xD031A9162D01088Cul, animDict))
        {
            Script.Wait(25);
        }
        ped.Task.PlayAnimation(animDict, animFile, 8f, -4f, duration, (AnimationFlags)flag, 1f);
        if (!Line) return;
        Script.Yield();
        while (Function.Call<bool>((Hash)0x1F0B79228E461EC9ul, ped, animDict, animFile, 3))
        {
            Script.Wait(25);
        }
    }

    public static void TaskPlayAnim(this Ped ped, string animDict, string animFile, int duration, ref bool ImmediatelyStop, bool Line = false, string IncludeStopAnimFile = "", int flag = 0)
    {
        Function.Call((Hash)0xD3BD40951412FEF6ul, animDict);
        while (!Function.Call<bool>((Hash)0xD031A9162D01088Cul, animDict))
        {
            Script.Wait(25);
        }
        ped.Task.PlayAnimation(animDict, animFile, 8f, -4f, duration, (AnimationFlags)flag, 1f);
        if (Line)
        {
            Script.Yield();
            while (Function.Call<bool>((Hash)0x1F0B79228E461EC9ul, ped, animDict, animFile, 3) && !ImmediatelyStop)
            {
                Script.Wait(25);
            }
        }

        if (!ImmediatelyStop) return;
        Function.Call((Hash)0x28004F88151E03E0ul, ped, animFile, animDict, true);
        if (IncludeStopAnimFile != string.Empty)
        {
            Function.Call((Hash)0x28004F88151E03E0ul, Main.P1.Character, IncludeStopAnimFile, animDict, true);
        }
    }

    public static void TaskPlayAnimAdvanced(this Ped ped, string animDict, string animFile, int duration, int flag, float animTime, Vector3 position, Vector3 rotation)
    {
        Function.Call((Hash)0xD3BD40951412FEF6ul, animDict);
        while (!Function.Call<bool>((Hash)0xD031A9162D01088Cul, animDict))
        {
            Script.Wait(25);
        }
        Function.Call((Hash)0x83CDB10EA29B370Bul, ped, animDict, animFile, position.X, position.Y, position.Z, rotation.X, rotation.Y, rotation.Z, 8f, -4f, duration, flag, animTime, 2, 0);
    }

    public static void TaskPlayAnimLoop(this Ped ped, string animDict, string animFile, int duration)
    {
        Function.Call((Hash)0xD3BD40951412FEF6ul, animDict);
        while (!Function.Call<bool>((Hash)0xD031A9162D01088Cul, animDict))
        {
            Script.Wait(25);
        }
        ped.Task.PlayAnimation(animDict, animFile, 8f, -4f, duration, AnimationFlags.Loop, 1f);
    }

    public static bool IsEntityPlayingAnim(this Ped ped, string animDict, string animFile)
    {
        return Function.Call<bool>((Hash)0x1F0B79228E461EC9ul, ped, animDict, animFile, 3);
    }

    public static void SetEntityAnimCurrentSpeed(this Ped ped, string animDict, string animFile, float speed)
    {
        Function.Call((Hash)0x28D1A16553C51776ul, ped, animDict, animFile, speed);
    }

    public static double GetEntityAnimCurrentTime(this Ped ped, string animDict, string animFile)
    {
        return Function.Call<double>((Hash)0x346D81500D088F42ul, ped, animDict, animFile);
    }

    public static void SetEntityAnimCurrentTime(this Entity entity, string animDict, string animFile, float time)
    {
        Function.Call((Hash)0x4487C259F0F70977ul, entity, animDict, animFile, time);
    }
}


